package org.hot100_1;

import org.algorithm.List.ListNode;

import java.util.HashSet;

public class leetcode26 {
    public ListNode detectCycle(ListNode head) {

        if (head==null || head.next==null){
            return null;
        }

        ListNode node = head;
        HashSet<ListNode> set= new HashSet<>();

        while (node!=null){
            if (set.contains(node)){
                return node;
            }
            set.add(node);
            node=node.next;
        }

        return null;
    }
}
